package com.cn.longdistancebusstation.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.callback.OnCustomViewClickListener;
import com.cn.longdistancebusstation.callback.OnSelectPassengerTypeListener;
import com.cn.longdistancebusstation.enumxx.PassengerType;
import com.cn.longdistancebusstation.model.Contacter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mucheng on 2018/4/26.
 */

public class SelectScenicPassengersAdapter extends BaseAdapter{
    private Context ctx;
    private List<Contacter> mDataSourceList;
    private OnCustomViewClickListener mOnCustomViewClickListener;
    public static Map<Integer, Boolean> isSelected;

    private OnSelectPassengerTypeListener mOnSelectPassengerTypeListener;
    public SelectScenicPassengersAdapter(Context ctx, List<Contacter> mDataSourceList) {
        this.ctx=ctx;
        this.mDataSourceList = mDataSourceList;
        init();
    }
    //这儿定义isSelected这个map是记录每个listitem的状态，初始状态全部为false
    private void init() {
        isSelected = new HashMap<Integer, Boolean>();
        for (int i = 0; i < mDataSourceList.size(); i++){
            isSelected.put(i, false);

        }

    }
    public OnSelectPassengerTypeListener getOnSelectPassengerTypeListener() {
        return mOnSelectPassengerTypeListener;
    }

    public void setOnSelectPassengerTypeListener(OnSelectPassengerTypeListener onSelectPassengerTypeListener) {
        mOnSelectPassengerTypeListener = onSelectPassengerTypeListener;
    }

    public OnCustomViewClickListener getOnCustomViewClickListener() {
        return mOnCustomViewClickListener;
    }

    public void setOnCustomViewClickListener(OnCustomViewClickListener onCustomViewClickListener) {
        mOnCustomViewClickListener = onCustomViewClickListener;
    }

    public List<Contacter> getDataSourceList() {
        return mDataSourceList;
    }

    public void setDataSourceList(List<Contacter> dataSourceList) {
        mDataSourceList = dataSourceList;
    }

    @Override
    public int getCount() {
        return mDataSourceList.size();
    }

    @Override
    public Object getItem(int i) {
        return mDataSourceList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(view==null){
            view = LayoutInflater.from(ctx).inflate(R.layout.view_scenic_select_passengers,null);
            holder=new ViewHolder(view);
            view.setTag(holder);
        }else{
            holder=(ViewHolder)view.getTag();
        }
        Contacter contacter= mDataSourceList.get(i);
        holder.mContacter = contacter;
        holder.mNameTextView.setText(contacter.getName());
        holder.mCardNoTextView.setText(contacter.getCardNo());
        holder.mTelephoneNumberTextView.setText(contacter.getTelephone());
        //
//        PassengerType passengerType = PassengerType.getPassengerType(typenumber);

        // 监听checkBox并根据原来的状态来设置新的状态
//        holder.mCheckbox.setChecked(isSelected.get(i));
        holder.mCheckbox.setChecked(contacter.getChoose()==null ? false : contacter.getChoose());

        return view;
    }

    public class ViewHolder{
        public TextView mNameTextView;
        public TextView mCardNoTextView;
        public Contacter mContacter;
        public CheckBox mCheckbox;
        public TextView mTelephoneNumberTextView;
        public ViewHolder(View itemView){
            mNameTextView =(TextView)itemView.findViewById(R.id.name_of_passenger);
            mCardNoTextView =(TextView)itemView.findViewById(R.id.number_of_idcard);
            mTelephoneNumberTextView=(TextView)itemView.findViewById(R.id.tourist_telephone_details);
            mCheckbox =(CheckBox)itemView.findViewById(R.id.cb_st);
            mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    mContacter.setChoose(b);
                }
            });
        }
    }
}
